<?php
/**
 * Description of Grade
 *
 * @author ROCU Fabien
 */
class Grade {
    public $grade_id;

    public $grade_nom;
    public $grade_abbr;
    public $grade_rang;

    function __construct( ) // aucune information dans le constructeur
    {

    }

    public function creer_grade(  ) // INSERT
    {
        global $_mysql;

        try {
            $query = "INSERT INTO grade VALUES ( null, '$this->grade_nom', '$this->grade_abbr', $this->grade_rang);";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {
                $this->grade_id = $_mysql->last_insert_id();
                return $this->grade_id;
            } else
                throw new Exception('Erreur Grade: la création du Grade a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_grade( $id ) // SELECT
    {
        global $_mysql;

        $this->grade_id = $id;

        try {
            $query = "SELECT * FROM grade WHERE grade_id = $id";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                $val = $_mysql->fetch_array($result);

                $this->grade_nom = $val['grade_nom'];
                $this->grade_abbr = $val['grade_abbr'];
                $this->grade_rang = $val['grade_rang'];

                if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
            }
            else
                throw new Exception('Erreur Grade: identifiant unique non respecté');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }

    }

    public function set_grade( $nom, $abbr, $rang ) {
        $this->grade_nom = $nom;
        $this->grade_abbr = $abbr;
        $this->grade_rang = $rang;

        if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
    }

    public function sauvegarder_grade() // UPDATE
    {
        global $_mysql;

        try {
            $query = "UPDATE grade SET grade_nom = '$this->grade_nom', grade_abbr = '$this->grade_abbr', grade_rang = $this->grade_rang WHERE grade_id = $this->grade_id;";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Grade: la modification du Grade a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function effacer_grade( $id ) // DELETE
    {
        $grade_id = $id;

        global $_mysql;

        try {
            $query = "DELETE FROM grade WHERE grade_id = $grade_id";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Grade: la suppression du Grade a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function qui_suis_je() // Return String
    {
        return " <br /> Grade --> ID: " . $this->grade_id . " Nom: " . $this->grade_nom . " Abbréviation: " . $this->grade_abbr . " Rang: " . $this->grade_rang . "<br />";
    }

}
?>
